package [[${data.pack}]];

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import [[${data.pack}]].BaseMapperParams.CompareColumn.CompareTypeEnum;

/**
 * Mapper参数
 * @author [[${data.author}]]
 * @Date [[${data.createDate}]]
 */
public class BaseMapperParams {
	private Integer offset;
	private Integer rows;
	private String orderByClause;

	private Set<Column> likeColumns = new HashSet<>();
	private Set<Column> betweenColumns = new HashSet<>();
	private Set<Column> inColumns = new HashSet<>();
	private Set<Column> columns = new HashSet<>();
	private Set<Column> compareColumns = new HashSet<>();
	private Set<Column> updateColumns = new HashSet<>();

	public BaseMapperParams() {
		super();
		setOffset(0);
		setRows([[${data.rows}]]);
	}
	
	public void clear() {
		likeColumns.clear();
		betweenColumns.clear();
		inColumns.clear();
		columns.clear();
		compareColumns.clear();
		updateColumns.clear();
		
		orderByClause = null;
	    setOffset(0);
	    setRows([[${data.rows}]]);
	}
	
	/**
	 * 设置页数
	 * @param page 当前页数
	 * @param size 每页多少行
	 */
	public void setPage(int page, int size) {
		if(page < 1) {
			page = 1;
		}
		setRows(size);
		setOffset((page - 1) * size);
	}

	public static class Column {
		private String name;
		private Object value;

		public Column() {
		}

		public Column(String name, Object value) {
			this.name = name;
			this.value = value;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Object getValue() {
			return value;
		}

		public void setValue(Object value) {
			this.value = value;
		}
		
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((name == null) ? 0 : name.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Column other = (Column) obj;
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equals(other.name))
				return false;
			return true;
		}
	}

	public static class BetweenColumn extends Column {
		private Object start;
		private Object end;

		public BetweenColumn(String name, Object start, Object end) {
			setName(name);
			this.start = start;
			this.end = end;
		}

		public Object getStart() {
			return start;
		}

		public void setStart(Object start) {
			this.start = start;
		}

		public Object getEnd() {
			return end;
		}

		public void setEnd(Object end) {
			this.end = end;
		}
	}

	public static class CompareColumn extends Column {
		private String compare;

		public enum CompareTypeEnum {
			GT, LT, GE, LE
		}

		public CompareColumn(String name, Object value, CompareTypeEnum type) {
			switch (type) {
			case GT:
				compare = ">";
				break;
			case LT:
				compare = "<";
				break;
			case GE:
				compare = ">=";
				break;
			case LE:
				compare = "<=";
				break;
			default:
				throw new RuntimeException("不支持此比较类型");
			}
			
			setName(name + " " + compare);
			setValue(value);
		}

		public String getCompare() {
			return compare;
		}

		public void setCompare(String compare) {
			this.compare = compare;
		}
	}

	public void addColumn(String name, Object value) {
		columns.add(new Column(name, value));
	}

	public void addLikeColumn(String name, Object value) {
		likeColumns.add(new Column(name, value));
	}

	public void addInColumns(String name, List value) {
		inColumns.add(new Column(name, value));
	}

	public void addBetweenColumn(String name, Object start, Object end) {
		betweenColumns.add(new BetweenColumn(name, start, end));
	}

	public void addCompareColumn(String name, Object value, CompareTypeEnum compare) {
		compareColumns.add(new CompareColumn(name, value, compare));
	}
	
	public void addUpdateColumn(String name, Object value) {
		updateColumns.add(new Column(name, value));
	}

	public Integer getOffset() {
		return offset;
	}

	public void setOffset(Integer offset) {
		this.offset = offset;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public String getOrderByClause() {
		return orderByClause;
	}

	public void setOrderByClause(String orderByClause) {
		this.orderByClause = orderByClause;
	}

	public Set<Column> getLikeColumns() {
		return likeColumns;
	}

	public void setLikeColumns(Set<Column> likeColumns) {
		this.likeColumns = likeColumns;
	}

	public Set<Column> getBetweenColumns() {
		return betweenColumns;
	}

	public void setBetweenColumns(Set<Column> betweenColumns) {
		this.betweenColumns = betweenColumns;
	}

	public Set<Column> getInColumns() {
		return inColumns;
	}

	public void setInColumns(Set<Column> inColumns) {
		this.inColumns = inColumns;
	}

	public Set<Column> getColumns() {
		return columns;
	}

	public void setColumns(Set<Column> columns) {
		this.columns = columns;
	}
	
	
	public Set<Column> getCompareColumns() {
		return compareColumns;
	}

	public void setCompareColumns(Set<Column> compareColumns) {
		this.compareColumns = compareColumns;
	}

	public Set<Column> getUpdateColumns() {
		return updateColumns;
	}

	public void setUpdateColumns(Set<Column> updateColumns) {
		this.updateColumns = updateColumns;
	}
}